<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.multysite.entity.Category"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@page import="com.multysite.entity.News"%>
<%@page import="com.multysite.entity.admin.Application"%>
<%@page import="java.util.TreeMap"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.ckeditor.CKEditorConfig"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
	News obj = (News) request.getAttribute("obj");
	String action = (String)request.getAttribute("action");
	List<Category> listCategory = (List<Category>)request.getAttribute("listCategory");
	List<Category> listParentCategory = (List<Category>)request.getAttribute("listParentCategory");
	if(listCategory==null){
		listCategory = new ArrayList<Category>();		
	}
	String returnUrl = "/admin/news/" + action;
	BlobstoreService blobService = BlobstoreServiceFactory.getBlobstoreService();
	if (obj == null) {
		obj = new News();
	}	
	CKEditorConfig settings = new CKEditorConfig();	
	settings.addConfigValue("height","200");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add News</title>
<style>
.error {
	color: red;
}
</style>
<script type="text/javascript" src="/admin/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="/sources/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$(".add-category").click(function(){
			$("#new-category").removeAttr("style");
			$("#btn-save").removeAttr("style");	
			$("#parent-category").removeAttr("style");				
		});		
		$("#btn-save").click(function(){
			var newCategory = $("#new-category").val();
			/* var path = /[^A-Za-z ]/g;
				if(path.test(newCategory)){
				alert("Category Title Cannot contains comma !");
			} */
			if(newCategory==""){
				alert("Please enter Category Title !");
			}else{
				var parent = $("#parent-category").val();
				$.get("/admin/category/process", {
					action : "add",
					title : newCategory,
					parent : parent
				}, function(data) {												
					if(data!=1){
						var arr = data.split(",");	
						var clone = $(".option-radio .option-item:first").clone();						
						clone.find("input").val(arr[0]);
						clone.find("input").attr("checked", "checked");
						clone.find("span").html(newCategory);
						clone.appendTo(".option-radio");
						$("#new-category").val("");
						if(arr[1]==1){
							clone = $(".select-parent .select-parent-item:first").clone();
							clone.val(arr[0]);
							clone.html(newCategory);
							clone.appendTo(".select-parent");	
						}		
					}else{
						alert("Exist Category");
					}				
				});
			}			
		});		
	});
</script>
</head>
<body>
	<h1>Please Enter News Information</h1>	
	<form enctype="multipart/form-data" action="<%=blobService.createUploadUrl(returnUrl)%>" method="post">		
		<table style="padding-left: 20px">
			<input type="hidden" name="action" value="<%=action%>">	
			<input type="hidden" name="alias" value="<%=obj.getAlias()%>">
			<tr>
				<td>Title</td>
				<td><input type="text" name="title" title="News Title" value="<%=obj.getTitle().replace("\"","'")%>" style="width: 400px"></td>				
			</tr>
			<tr>
				<td>Content :</td>
				<td>
					<textarea cols="80" rows="5" id=content name="content" class="ckeditor"><%=obj.getContent()%></textarea>												
				</td>				
			</tr>
			
			<tr>
				<td>Category :</td>
				<td>
					<div class="option-radio">
					<%
						for(Category cate: listCategory){
							%>
							<div class="option-item">
								<input <%=(obj.getCategoryAlias().equals(cate.getAlias())?"checked='checked'":"") %> type="radio" name="category" value="<%=cate.getAlias()%>"/><span><%=cate.getTitle() %></span>
							</div>					
							<%
						}
					%>		
					</div>			
					<div>
						
					</div>					
					<a href="#" class="add-category">(add category)</a>
					<br>					
					<input id="new-category" style="display: none;" type="text" name="" title="Category" value="<%=obj.getCategory()%>">
					<br>
					<select id="parent-category" name="parent-category" style="display: none;" class="select-parent">										
						<option value="0">--Parent Category--</option>
						<%
							for(Category cate: listParentCategory){
							%>
							<option class="select-parent-item" value="<%=cate.getAlias()%>"><%=cate.getTitle() %></option>
							<%
							}
						%>
					</select>
					<input id="btn-save" style="display: none;" type="button" value="Add">
				</td>				
			</tr>
			<tr>
				<td>Source :</td>
				<td><input type="text" name="source" title="source" value="<%=obj.getSource()%>" style="width: 400px"></td>			
			</tr>
			<%
				String tags = "";
				if(obj.getTags().size()>0){
					for(String tag: obj.getTags()){
						tags += tag.split(",")[1];
					}
				}
			%>
			<tr>				
				<td>Tags :</td>
				<td>
					<input type="text" name="tags" title="tags" value="<%=tags%>">				
				</td>				
			</tr>			
			<tr>
				<td>Cover Image :</td>
				<td>					
					<%
					if(obj.getThumb().length()>0){
						%>
						<img src="<%=obj.getThumb() %>"  style="width:200px">
						<%
					}										
				%>	
				<input type="file" name="img" class="smallinput">
				</td>						
			</tr>
			
			<tr>
				<td></td>
				<td>
					<input type="submit" value="Submit" title="Submit">
				</td>
			</tr>
			<tr>
				<td></td>
				<td>
					
				</td>				
			</tr>
		</table>		
	</form>
	<br>
	<a href="/admin/news/list">List News</a>
</body>
</html>